package com.letoken.platform.app.ApiResponseEnum;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wyl
 * @Description TODO
 * @date 2023/1/9 18:24
 * @Version 1.0
 */
public class PropertiesDemo {


    public static void main(String[] args) throws IOException {
        List<String> valueList = new ArrayList<>();
        valueList.add("网络异常，请稍后重试！");
        valueList.add("Network exception, please try again later!");
        valueList.add("ネットワーク例外です。後でもう一度お試しください！");
        valueList.add("네트워크 예외입니다. 나중에 다시 시도하십시오！");
        addProperties("80205002", valueList);
//        sortProperties();

        //传入路径，初始化配置文件，
//        PropertiesUtils util = new PropertiesUtils("app/src/main/resources/static/i18n/message_zh_CN.properties");
//        PropertiesUtils util = new PropertiesUtils("application-debug.properties");

    }

    static void updateProperties(String key, List<String> valueList) throws IOException {
        List<PropertiesUtils> utilList = getLanguagesProperties();
        if (valueList.size() != utilList.size()) {
            System.out.println("长度不对");
            return;
        }

        for (int i = 0; i < utilList.size(); i++) {
            String res = utilList.get(i).get(key);
            if (StringUtils.isBlank(res)){
                System.out.println("修改失败，没有这个配置！");
            }else {
                utilList.get(i).set(key, valueList.get(i));
            }
        }


    }


    static void addProperties(String key, List<String> valueList) throws IOException {
        List<PropertiesUtils> utilList = getLanguagesProperties();
        if (valueList.size() != utilList.size()) {
            System.out.println("长度不对");
            return;
        }

        for (int i = 0; i < utilList.size(); i++) {
            utilList.get(i).add(key, valueList.get(i));
        }
    }

    /**
     * 排序错误码
     * @throws IOException
     */
    static void sortProperties() throws IOException {
        List<PropertiesUtils> utilList = getLanguagesProperties();
        for (PropertiesUtils utils : utilList) {
            Map<String, String> all = utils.getAll();
            System.out.println(all);
            Map<String, String> sortMap = all.entrySet().stream()
                    .sorted(Map.Entry.comparingByKey())
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            Map.Entry::getValue,
                            (oldVal, newVal) -> oldVal,
                            LinkedHashMap::new
                    ));
            System.out.println(sortMap);
            List<String> lineList = new ArrayList<>();
            for (String key : sortMap.keySet()) {
                lineList.add(key + "=" + sortMap.get(key));
            }
            String[] lines = lineList.toArray(new String[]{});
            utils.commit(lines);
        }


    }

    static List<PropertiesUtils> getLanguagesProperties() throws IOException {
        PropertiesUtils zhCNUtil = new PropertiesUtils("app/src/main/resources/static/i18n/message_zh_CN.properties");
        PropertiesUtils enUSUtil = new PropertiesUtils("app/src/main/resources/static/i18n/message_en_US.properties");
        PropertiesUtils jaJPUtil = new PropertiesUtils("app/src/main/resources/static/i18n/message_ja_JP.properties");
        PropertiesUtils koKRUtil = new PropertiesUtils("app/src/main/resources/static/i18n/message_ko_KR.properties");
        List<PropertiesUtils> utilList = new ArrayList<>();
        utilList.add(zhCNUtil);
        utilList.add(enUSUtil);
        utilList.add(jaJPUtil);
        utilList.add(koKRUtil);
        return utilList;
    }

}
